package com.appview;

import com.dao.CurriculumDao;
import com.dao.StudentDao;
import com.dao.TeacherDao;
import com.model.Course;
import com.model.Curriculum;
import com.model.Student;
import com.model.Teacher;
import com.util.Config;
import sun.applet.Main;
import sun.plugin.dom.core.CoreConstants;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

/**
 * Created by Qi_Huang on 2015/11/1.
 * 此类用于table的各项操作和信息修改
 */
public class TableInfoTools {

    private static String major = null;
    //显示查询信息

    //获得table某一行信息，返回List，用于修改信息
    public static List<String> getModifyInfo(JTable jTable) {
        int row = jTable.getSelectedRow();
        List<String> info = new ArrayList<>();
        for (int i = 0; i <= jTable.getColumnCount() - 1; i++) {
            info.add((String) jTable.getValueAt(row, i));
        }
        return info;
    }

    //删除table某一行
    public static void deleteInfoRow(JTable jTable) {
        int row = jTable.getSelectedRow();
        String accout = jTable.getValueAt(row, 0).toString();
//		System.out.println(name);
        switch (Config.getType()) {
            case 0:
                DataDeal.deleteTeacher(accout);
                break;
            case 1:
                DataDeal.deleteStudent(accout);
                break;
        }
    }

    //显示列表所有信息
    public static void printInfo(DefaultTableModel tableModel) {
        switch (Config.getType()) {
            case 0:
                TeacherDao teacherDao =TeacherDao.getInstance();
                List<Vector> list = teacherDao.vectorList();
                for (Vector vector: list) {
                    tableModel.addRow(vector);
                }
                break;

            case 1:
                StudentDao studentDao = StudentDao.getInstance();
                List<Vector> list1 = studentDao.vectorList();
                for (Vector vector: list1) {
                    tableModel.addRow(vector);
                }
                break;
        }
    }

    //管理员确定添加或修改信息
    public static void changeInfo(int type, JTextField[] jTextFields) {
        switch (Config.getType()) {
            case 0:
                Teacher teacher;
                TeacherDao teacherDao = TeacherDao.getInstance();
                if (type == 0) {
                    teacher = new Teacher();
                    teacher.setAccount(jTextFields[0].getText());
                    teacher.setPassword(jTextFields[0].getText());

                } else {
                    teacher = teacherDao.load(jTextFields[0].getText().toString());
                    System.out.println(teacher.getPassword() + teacher.getAccount());
                }
                teacher.setId(0);
                teacher.setName(jTextFields[1].getText());
                teacher.setAge(jTextFields[2].getText());
                teacher.setSex(jTextFields[3].getText());
                teacher.setBirth(jTextFields[4].getText());
                teacher.setPlace(jTextFields[5].getText());
                teacher.setStatus(jTextFields[6].getText());
                teacher.setCollege(jTextFields[7].getText());
                teacher.setMajor(jTextFields[8].getText());
                teacher.setWorkYear(jTextFields[9].getText());
                teacher.setJob(jTextFields[10].getText());

                if (type == 0) {
                    DataDeal.addTeacher(teacher);
                } else {
                    DataDeal.updateTeacher(teacher);
                }


                break;
            case 1:
                Student student;
                StudentDao studentDao = StudentDao.getInstance();
                if (type == 0) {
                    student = new Student();
                    student.setAccount(jTextFields[0].getText().toString());
                    student.setPassword(jTextFields[0].getText());
                }else {
                    student = studentDao.loadByAccount(jTextFields[0].getText().toString());
                }
                student.setId(0);

                student.setName(jTextFields[1].getText().toString());
                student.setAge(jTextFields[2].getText().toString());
                student.setSex(jTextFields[3].getText().toString());
                student.setBirth(jTextFields[4].getText().toString());
                student.setFrom(jTextFields[5].getText().toString());
                student.setStatus(jTextFields[6].getText().toString());
                student.setCollege(jTextFields[7].getText().toString());
                student.setMajor(jTextFields[8].getText().toString());

                if (type == 0) {
                    DataDeal.addStudent(student);
                } else {
                    DataDeal.updateStudent(student);
                }


                break;
        }

    }

    //教师或学生修改信息
    public static void userChangeInfo(JTextField[] jTextFields) {
        switch (Config.getLoginType()) {
            case 0:
                Teacher teacher;
                TeacherDao teacherDao = TeacherDao.getInstance();

                teacher = teacherDao.load(jTextFields[0].getText().toString());

                teacher.setId(0);
                teacher.setName(jTextFields[1].getText());
                teacher.setAge(jTextFields[2].getText());
                teacher.setSex(jTextFields[3].getText());
                teacher.setBirth(jTextFields[4].getText());
                teacher.setPlace(jTextFields[5].getText());
                teacher.setStatus(jTextFields[6].getText());
                teacher.setCollege(jTextFields[7].getText());
                teacher.setMajor(jTextFields[8].getText());
                teacher.setWorkYear(jTextFields[9].getText());
                teacher.setJob(jTextFields[10].getText());

                DataDeal.updateTeacher(teacher);

                break;
            case 1:
                Student student;
                StudentDao studentDao = StudentDao.getInstance();

                student = studentDao.loadByAccount(jTextFields[0].getText().toString());
                student.setId(0);

                student.setName(jTextFields[1].getText().toString());
                student.setAge(jTextFields[2].getText().toString());
                student.setSex(jTextFields[3].getText().toString());
                student.setBirth(jTextFields[4].getText().toString());
                student.setFrom(jTextFields[5].getText().toString());
                student.setStatus(jTextFields[6].getText().toString());
                student.setCollege(jTextFields[7].getText().toString());
                student.setMajor(jTextFields[8].getText().toString());

                DataDeal.updateStudent(student);
                break;
        }

    }

    //添加课表
    public static void addCourse(int row, int col, String s) {

        MainView.getMainView().jTable1.setValueAt(s, row, col);
    }

    //获取课表数组并保存到Curriculum
    public static void saveCourse(JTable jTable1) {
        Curriculum curriculum = new Curriculum();
//        String[][] courses = new String[6][7];
        for (int i = 1; i < 8; i++) {
            for (int j = 0; j < 6; j++) {
                if (jTable1.getValueAt(j, i) != null) {
//                    courses[j][i - 1] = (String) jTable1.getValueAt(j, i);
                    String info = (String) jTable1.getValueAt(j, i);
//                    System.out.println(info);
                    info = info.replace("<html>", "");
                    info.trim();
                    String[] infos = info.split("<br>");
//                    System.out.println(infos[0]);
//                    System.out.println(infos.length);
                    Course course = new Course();
                    course.setTeachet(infos[1]);
                    course.setPlace(infos[2]);
                    course.setName(infos[0]);
                    course.setI(j);
                    course.setJ(i - 1);

                    if (Config.getType() == 0){
                        String teacher = TeacherDao.getInstance().findByName(infos[1]).getAccount();
                        curriculum.setTeacher(teacher);
                        curriculum.setName(TeacherDao.getInstance().findByName(infos[1]).getMajor());
                    } else if (Config.getType() == 1) {
                        major = (String) MainView.getMainView().jcb.getSelectedItem();
                        curriculum.setName(major);
                    }

                    curriculum.getCourses()[j][i - 1] = course;
                }
            }
        }

        //提交课表

        CurriculumDao curriculumDao = CurriculumDao.getInstance();
        if (Config.getType() == 0) {
            curriculumDao.add(curriculum);
        } else if (Config.getType() == 1) {
            //添加学生专业班级课表
            curriculumDao.addStuCurr(curriculum);
        }

        JOptionPane.showMessageDialog(MainView.getMainView(), "提交成功！", "Tip", JOptionPane.DEFAULT_OPTION);
    }


    //显示教师或学生班级 课表
    public static void printTeaCourse(JTable jTable1, String teacherName) {
        //先清空课表显示
        for (int i = 1; i < 8; i++) {
            for (int j = 0; j < 6; j++) {
                jTable1.setValueAt(null, j, i);
            }
        }

        CurriculumDao curriculumDao = CurriculumDao.getInstance();
        Curriculum curriculum = null;
        if (Config.getType() == 0)
            curriculum = curriculumDao.loadByTeacher(TeacherDao.getInstance().findByName(teacherName).getAccount());
        else if (Config.getType() == 1){
            //学生专业课表
            major = (String) MainView.getMainView().jcb.getSelectedItem();
            curriculum = curriculumDao.loadByName(major);
        }


        String info, place, course;
        try {
            for (int i = 1; i < 8; i++) {
                for (int j = 0; j < 6; j++) {
                    if (curriculum.getCourses()[j] [i - 1] != null) {
                        place = curriculum.getCourses()[j][i - 1].getPlace();
                        course = curriculum.getCourses()[j][i - 1].getName();
                        info = course + " " + teacherName + " " + place;
                        info = info.trim();
                        info = info.replaceAll("\\s+", "<br>");
                        info = "<html>" + info;
                        jTable1.setValueAt(info, j, i);
                    }
                }
            }
        } catch (NullPointerException e) {
            JOptionPane.showMessageDialog(MainView.getMainView(), "尚未录入课表", "Error", JOptionPane.ERROR_MESSAGE);
        }

    }

    //显示个人课表
    public static void printCourse(JTable jTable1, String teacherName) {
        //先清空课表显示
        for (int i = 1; i < 8; i++) {
            for (int j = 0; j < 6; j++) {
                jTable1.setValueAt(null, j, i);
            }
        }
        CurriculumDao curriculumDao = CurriculumDao.getInstance();
        Curriculum curriculum = null;

        if (Config.getLoginType() == 0)
            curriculum = curriculumDao.loadByTeacher(TeacherDao.getInstance().findByName(teacherName).getAccount());
       else if (Config.getLoginType() == 1) {
            curriculum = curriculumDao.loadByStudent(UserView.getUserView().accout);
        }



        String info, place, course;
        try {
            for (int i = 1; i < 8; i++) {
                for (int j = 0; j < 6; j++) {
                    if (curriculum.getCourses()[j] [i - 1] != null) {
                        place = curriculum.getCourses()[j][i - 1].getPlace();
                        course = curriculum.getCourses()[j][i - 1].getName();
                        info = course + " " + teacherName + " " + place;
                        info = info.trim();
                        info = info.replaceAll("\\s+", "<br>");
                        info = "<html>" + info;
                        jTable1.setValueAt(info, j, i);
                    }
                }
            }
        } catch (NullPointerException e) {
            JOptionPane.showMessageDialog(MainView.getMainView(), "尚未录入课表", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

}
